package com.leff.cards.data;

import java.util.ArrayList;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

import android.util.Log;

import com.leff.cards.util.Secrets;
import com.leff.cards.util.Dictionary;

public class QueryCard extends AbstractCard implements Secrets {

	private static final String BLANK = "______";
	private static final String CAP = "";
	
	private int mRequiredInputs;
	private ResponseCard[] mResponses;
	
	private long mId;
	private String mText;
	
	public QueryCard() {
	}
	
	public void setText(String text) {
		mText = text;
		
		mRequiredInputs = 0;
		while(text.contains("#blank#")) {
			
			text = text.replaceFirst("#blank#", BLANK);
			mRequiredInputs++;
		}
		if(mRequiredInputs == 0) {
			mRequiredInputs = 1;
		}
		
		mResponses = new ResponseCard[mRequiredInputs];
	}
	
	public void setResponse(int input, ResponseCard response) {
		mResponses[input] = response;
	}
	public void setResponses(CompositeResponseCard responses) {
		
		ArrayList<ResponseCard> cards = responses.getResponses();
		for(int i = 0; i < cards.size() && i < mResponses.length; i++) {
			mResponses[i] = cards.get(i);
		}
	}
	
	public boolean hasResponses() {
		for(int i = 0; i < mResponses.length; i++) {
			if(mResponses[i] != null) {
				return true;
			}
		}
		return false;
	}
	
	public void reset() {
		for(int i = 0; i < mResponses.length; i++) {
			mResponses[i] = null;
		}
	}
	public boolean isCompleted() {
		for(int i = 0; i < mResponses.length; i++) {
			if(mResponses[i] == null) {
				return false;
			}
		}
		return true;
	}
	
	public long getId() {
		return mId;
	}
	
	public String getText() {
		return getTextWithResponses(null);
	}
	
	public String getTextWithResponses() {
		return getTextWithResponses(mResponses);
	}
	
	private String getTextWithResponses(ResponseCard[] cards) {
		
		String text = mText;
		for(int i = 0; i < mRequiredInputs; i++) {
			String rep = BLANK;
			if(cards != null && cards[i] != null) {
				rep = mResponses[i].getText();
				if(rep.endsWith(".")) {
					rep = rep.substring(0, rep.length() - 1);
				}
			}
			text = text.replaceFirst("#blank#", rep.toUpperCase());
		}
		
		return CAP + text + CAP;
	}
	
	public int getRequiredNumberOfInputs() {
		return mRequiredInputs;
	}
	public ResponseCard[] getResponses() {
		return mResponses;
	}
	
	public static QueryCard fromDictionary(Dictionary dict) {
		
		QueryCard qc = new QueryCard();
		
		qc.mId = (Long)dict.get("id");
		qc.setText((String)dict.get("text"));
		
		return qc;
	}
	
	public static QueryCard fromJSONString(String json) {
		
		JSONParser parser = new JSONParser();
		
		QueryCard qc = new QueryCard();
		
		try {
			JSONObject obj = (JSONObject)parser.parse(json);
			
			qc.mId = (Long)obj.get("id");
			qc.setText((String)obj.get("text"));
			
		} catch(Exception e) {
			Log.v(TAG, "Error Parsing Server Response:", e);
		}
		return qc;
	}
}
